package com.face.dao;

import com.face.common.Page;
import com.face.entity.Remark;
import com.face.mapper.RemarkMapper;
import com.face.wrapper.RemarkWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class RemarkDao {
    @Autowired
    private RemarkMapper remarkMapper;

    public int saveRemark(Remark remark) {
        int i = remarkMapper.saveRemark(remark);
        if (i > 0)
            return remark.getRemarkId();
        return -1;
    }

    public List<Remark> getRemarksByUserId(String userId) {
        return remarkMapper.getRemarksByUserId(userId);
    }

    public List<Remark> getRemarks(String userId, String start, String end) {
        return remarkMapper.getRemarks(userId, start, end);
    }

    public List<Remark> getRecentRemarks(String userId, Integer limit) {
        return remarkMapper.getRecentRemarks(userId, limit);
    }

    public Page<RemarkWrapper> listAll(String shopId, Integer page, Integer limit, String order) {
        PageHelper.startPage(page, limit);
        List<RemarkWrapper> list = remarkMapper.listAll(shopId, order);
        PageInfo<RemarkWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Page<RemarkWrapper> list(String shopId, Integer page, Integer limit, String start, String end, String order) {
        PageHelper.startPage(page, limit);
        List<RemarkWrapper> list = remarkMapper.list(shopId, start, end, order);
        PageInfo<RemarkWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Integer count(String shopId, String start, String end) {
        return remarkMapper.count(shopId, start, end);
    }

    public Integer count(String shopId) {
        return remarkMapper.countAll(shopId);
    }

    public Page<RemarkWrapper> getNewestRemarkList(String shopId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<RemarkWrapper> list = remarkMapper.getNewestRemarkList(shopId);
        PageInfo<RemarkWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Page<RemarkWrapper> getNewestRemarkList(String shopId, Integer page, Integer limit, String start, String end) {
        PageHelper.startPage(page, limit);
        List<RemarkWrapper> list = remarkMapper.getNewestRemarks(shopId, start, end);
        PageInfo<RemarkWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Page<Map<String, Object>> getRemarkCountList(String shopId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> list = remarkMapper.getRemarkCountList(shopId);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Page<Map<String, Object>> getRemarkCountList(String shopId, Integer page, Integer limit, String start, String end) {
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> list = remarkMapper.getRemarkCounts(shopId, start, end);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }

    public Page<RemarkWrapper> listAllByUserId(String shopId, String userId, Integer page, Integer limit, String start, String end) {
        PageHelper.startPage(page, limit);
        List<RemarkWrapper> list;
        if (start == null || start.isEmpty() || end == null || end.isEmpty()) {
            list = remarkMapper.listAllByUserId(shopId, userId);
        } else {
            list = remarkMapper.listAllByUserIdAndTime(shopId, userId, start, end);
        }
        PageInfo<RemarkWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }


    public List<Map<String, Object>> dailyCount(String shopId, String start, String end) {
        return remarkMapper.dailyCount(shopId, start, end);
    }

    public List<Map<String, Object>> monthlyCount(String shopId, String start, String end) {
        return remarkMapper.monthlyCount(shopId, start, end);
    }

    public List<Map<String, Object>> dailyRemarkCount(String shopId, String start, String end) {
        return remarkMapper.dailyRemarkCount(shopId, start, end);
    }

    public List<Map<String, Object>> monthlyRemarkCount(String shopId, String start, String end) {
        return remarkMapper.monthlyRemarkCount(shopId, start, end);
    }

    public List<RemarkWrapper> getRecentRemarksByShopId(String shopId, int limit) {
        return remarkMapper.getRecentRemarksByShopId(shopId, limit);
    }

    public List<Map<String, String>> getRemarksForWx(String userId, String start, String end, String keyword) {
        return remarkMapper.getRemarksForWx(userId, start, end, keyword);
    }
}
